<?php
/**
 * @file $HeadURL: imagelibrary.php $
 * @author $Author: LiangJian (liangjian@haibao.com) $
 * @date $Date: 2014-12-22 下午12:51:56 $
 * @brief 
 */
namespace haibao\cms\business;

use \haibao\frame\data\query\Condition;

class ImageLibrary extends \haibao\cms\business\Base{
	
    public static function getStarIdByImageId($imageId){
        $data = new \haibao\cms\data\ImageLibrary();
        return $data->getStarIdByImageId($imageId);
    }
    
    public static function  updateImageIsFashionWear($imageId){
        $data = new \haibao\cms\data\ImageLibrary();
        $data->updateImageIsFashionWear($imageId);
    }
    
	public static function setImageIdsStorage($name,$userId,$ids){
		$data = new \haibao\cms\data\ImageLibrary();
		
		$data->setImageIdsStorage($name,$userId,$ids);
	}
	
	public static function getImageIdsStorage($name,$userId){
		$data = new \haibao\cms\data\ImageLibrary();
		
		return $data->getImageIdsStorage($name,$userId);
	}

	public static function delImageIdsStorage($name,$userId){
		$cache = new \haibao\cms\cache\ImageLibrary();
		$cache->delImageIdsStorage($name,$userId);
	}
	
	public static function searchImagesByKeyword($filter){
		$data = new \haibao\cms\data\ImageLibrary();
		
		return $data->searchImagesByKeyword($filter);
	}
	
	public static function getImageTagsById($imageId){
		$data = new \haibao\cms\data\ImageLibrary();
		
		return $data->getImageTagsById($imageId);
	}

	public static function getImageTagsByImageIds($imageIds){
		$data = new \haibao\cms\data\ImageLibrary();

		return $data->getImageTagsByImageIds($imageIds);
	}
	
	public static function getImageById($imageId){
		if(!$imageId){
			throw new BusinessException('图片ID传递错误');
		}
		$data = new \haibao\cms\data\ImageLibrary();
		$result = $data->getImageById($imageId);
		return current($result);
	}
	
	/**
	 * 通过图片ID得实体（图片实体、图片来源、图片标签）
	 * @param unknown_type $imageId
	 * @throws BusinessException
	 * @return Ambigous <NULL, unknown>
	 */
	public static function getImageModelById($imageId){
		if(!$imageId){
			throw new BusinessException('图片ID传递错误');
		}
		$imageFilter = new \haibao\cms\model\filter\ImageLibrary();
		$imageFilter->select(array(
				$imageFilter::CONDITION_FIELD_NAME_ID,
				$imageFilter::CONDITION_FIELD_NAME_FILENAME,
				$imageFilter::CONDITION_FIELD_NAME_WIDTH,
				$imageFilter::CONDITION_FIELD_NAME_HEIGHT,
				$imageFilter::CONDITION_FIELD_NAME_SOURCE,
				$imageFilter::CONDITION_FIELD_NAME_DESCRIPTION,
				$imageFilter::CONDITION_FIELD_NAME_IS_WATER
		));
		
		$imageFilter->where(\haibao\cms\model\filter\ImageLibrary::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL, $imageId);
		
		$imageSourceFilter = new \haibao\cms\model\filter\ImageSource();
		$imageSourceFilter->select(array(
				$imageSourceFilter::CONDITION_FIELD_NAME_NAME,
		));
		$imageFilter->leftJoin($imageSourceFilter, null, $imageFilter::CONDITION_FIELD_NAME_SOURCE, $imageSourceFilter::CONDITION_FIELD_NAME_ID, $imageFilter::CONDITION_FIELD_NAME_SOURCE_NAME,false);
		
		$imageTagFilter = new \haibao\cms\model\filter\ImageTag();
		$imageTagFilter->select(array(
				$imageTagFilter::CONDITION_FIELD_NAME_TAGID
		));
		$imageFilter->leftJoin($imageTagFilter, null, $imageFilter::CONDITION_FIELD_NAME_ID, $imageTagFilter::CONDITION_FIELD_NAME_IMAGEID, $imageFilter::CONDITION_FIELD_NAME_TAG_ID);
		
		$data = new \haibao\cms\data\ImageLibrary();
		return $data->getOne($imageFilter);
	}
	
	/**
	 * @param int/array $id
	 */
	public static function getImagesById($imageIds){
		if(!$imageIds){
			throw new BusinessException('图片ID传递错误');
		}
		$data = new \haibao\cms\data\ImageLibrary();
		return $data->getImageById((array)$imageIds);
	}
	/**
	 * 通过图片id得图片地址
	 * @param array/int $imageId
	 * @return array($imageId=>$url)
	 */
	public static function getImageUrlById($imageIds){
		$data = new \haibao\cms\data\ImageLibrary();
		return $data->getImageUrlById($imageIds);
	}
	
	public static function downloadOneImage($imageId){
		$models = self::getImagesById(array($imageId));
		if(!$models){
			throw new BusinessException('下载失败');
		}
		
		$model = array_pop($models);
		self::checkImage($model->PicId,$model->Filename);
		$filePath = self::getSaveImagePath($model->Filename);
		
		return $filePath;
	}
	
	/**
	 * 批量下载图片
	 * @param int/array $imageIds
	 * @throws BusinessException
	 */
	public static function downloadImage($imageIds){
		self::checkOperation(self::FUNC_CODE_IMAGE_LIBRARY_DOWNLOAD);
		
		$models = self::getImagesById((array)$imageIds);
		if(!$models){
			throw new BusinessException('下载失败');
		}
		$saveFilename = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::IMAGE_DOWN_BASE_PATH);
		if(file_exists($saveFilename)){
			chmod($saveFilename, 0777);
			unlink($saveFilename);
		}
		$zip = new \ZipArchive();
		$res = $zip->open($saveFilename, \ZIPARCHIVE::CREATE);
		
		if ( $res === TRUE) {
			foreach ($models as $model){
				//self::checkImage($model->PicId, $model->Filename);
				$filePath = self::getSaveImagePath($model->Filename);
				if(file_exists($filePath)){
					$zip->addFile($filePath, basename($filePath));
				}
			}
			$zip->close();
			self::saveOperateHistory(self::FUNC_CODE_IMAGE_LIBRARY_DOWNLOAD, $models);
		}
		return $saveFilename;
	}
	
	/**
	 * 筛选图片
	 */
	public static function getImageLibraryByQuery($query){
		self::checkOperation(self::FUNC_CODE_IMAGE_LIBRARY_MANAGE);
		
		if(!$query){
			$query = new \haibao\cms\model\filter\ImageLibrary();
		}
		$data = new \haibao\cms\data\ImageLibrary();
		$imageList = $data->getImageLibraryByQuery($query);
		self::dataConvert($imageList);
		
		return $imageList;
	}
	
	public static function getImageOneByFilter($query){
		if(!$query){
			$query = new \haibao\cms\model\filter\ImageLibrary();
		}
		$data = new \haibao\cms\data\ImageLibrary();
		return $data->getOne($query);
	}
	
	public static function getImageAllByFilter($query){
		if(!$query){
			$query = new \haibao\cms\model\filter\ImageLibrary();
		}
		$data = new \haibao\cms\data\ImageLibrary();
		return $data->getAll($query);
	}
	/**
	 * @param \haibao\cms\model\data\ImageLibrary $model
	 */
	public static function saveUpload($file, $upSource, $isTemp = true){
		$storagePath = self::getUploadStoragePath($file['name']);
		$data = new \haibao\cms\data\ImageLibrary();
		
		$userId = self::getCurrentUser()->Id;
		$userName = self::getCurrentUser()->UserName;
		
		$picId = $data->saveImage($file['tmp_name'], $storagePath, $userId, $upSource,$userName, $isTemp);
		self::generateImage($storagePath, file_get_contents($file['tmp_name']));
		
		self::saveOperateHistory(self::FUNC_CODE_IMAGE_LIBRARY_UPLOAD, $file);
		
		return array(
			'id' => $picId,
			'imageUrl' => $storagePath
		);
	}
	
	/**
	 * @param \haibao\cms\model\data\ImageLibrary $model
	 */
	public static function saveUploadPath($file, $uploadFile , $upSource, $isTemp = true){
		$uploadNewUrl = $uploadList = $uploadListArr = array();
		$uploadCache = new \haibao\cms\cache\ImageLibrary();
		$uploadFile['chunk'] = isset($uploadFile['chunk']) ? $uploadFile['chunk'] : 0;
		$uploadFile['chunks'] = isset($uploadFile['chunks']) ? $uploadFile['chunks'] : 1;
		$userId =  self::getCurrentUser()->Id;
		/* 开始上传文件，删除垃圾数据 chunk为0 */
		if($uploadFile['chunk']==0){
			$uploadList = $uploadCache->getUploadUrl($userId);
			self::delWastedata($uploadList);
			$uploadCache->delUploadUrl($userId);
			$uploadCache->delUploadTrunk($userId);
		}
		$storagePath = self::getUploadStoragePath($file['name']);
// 		self::getFullUploadUrl();
		if(!$file['tmp_name']){
			throw new BusinessException('临时文件不存在！重新上传');
		}
		
		self::generateUpload($storagePath, file_get_contents($file['tmp_name']));
		
		$uploadNewUrl['userId'] = self::getCurrentUser()->Id;
		$uploadNewUrl['chunk'] = $uploadFile['chunk'];
		$uploadNewUrl['url'] = $storagePath;
		
		$uploadCache->setUploadUrl($userId,$uploadNewUrl);
		
		$uploadCache->setUploadTrunk($userId,$uploadFile['chunks']);
		
		if($uploadFile['chunk']+1 == $uploadFile['chunks']){
			$uploadListArr = $uploadCache->getUploadUrl($userId);
			$trunks = $uploadCache->getUploadTrunk($userId);
			if($uploadListArr){
				$uploadListArr = self::arraySort($uploadListArr, 'chunk');
// 				print_r($uploadListArr);exit;
				$basePath = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::IMAGE_UPLOAD_BASE_PATH).date('Y').DIRECTORY_SEPARATOR.date('md').DIRECTORY_SEPARATOR;
				$fullPath = $basePath.microtime(true);
				$extension = pathinfo($file['name']);
				$fullPath .= '.' . strtolower(isset($extension['extension']) ? $extension['extension'] : 'jpg');
				$url = str_replace(array('/', '\\'), '/', $fullPath);
				$handle = fopen(\haibao\frame\Environment::path().$url,"a");
				foreach ($uploadListArr as $k=>$v){
					$path = \haibao\frame\Environment::path().'/tmppic'.$v['url'];
					if(file_exists($path)){
						$fileDepart = fopen($path,"r");
						$readFile = fread($fileDepart,filesize($path));
						fwrite($handle,$readFile);
						fclose($fileDepart);
						unset($fileDepart,$readFile);
						unlink($path);
					}else{
						throw new \Exception("文件不存在->".$path);
					}
				}
				fclose($handle);
				$data = new \haibao\cms\data\ImageLibrary();
				$userId = self::getCurrentUser()->Id;
				$userName = self::getCurrentUser()->UserName;
				$picId = $data->saveImage(\haibao\frame\Environment::path().$url, $url, $userId, $upSource,$userName, $isTemp);
				
				self::saveOperateHistory(self::FUNC_CODE_IMAGE_LIBRARY_UPLOAD, $file);
				
				return array(
						'id' => $picId,
						'imageUrl' => $url
				);

			}
		}
	
		return array(
				'id' => 0,
				'imageUrl' => ''
		);
	}
	
	/**
	 * 二维数组按照某以键值排序
	 * arraySort($array, $keys, $type = SORT_DESC)
	 */
	private static function arraySort($array, $keys, $type = SORT_ASC) {
		$keysValue = $newArray = array();
		foreach ($array as $k => $v) {
			$keysValue[$k] = $v[$keys];
		}
		$type == SORT_ASC ? asort($keysValue) : arsort($keysValue);
		reset($keysValue);
		foreach ($keysValue as $k => $v) {
			array_push($newArray, $array[$k]);
		}
		return $newArray;
	}
	
	private static function delWastedata($uploadList){
		foreach ($uploadList as $k=>$v){
			$path = \haibao\frame\Environment::path().'/tmppic'.$v['url'];
			file_exists($path)?unlink($path):'';
		}
	}
	
	public static function getFullUploadUrl(){
		$path = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::IMAGE_UPLOAD_BASE_PATH).date('Y').DIRECTORY_SEPARATOR.microtime(true);
		$savePath = self::getSaveImagePath(str_replace(array('/', '\\'), '/', $path));
		self::createDir($savePath);
	}
	
	public static function generateUpload($path, $stream){
		$savePath = self::getSaveUploadPath($path);
		self::createDir($savePath);
		file_put_contents($savePath, $stream, LOCK_EX);
	}
	
	/**
	 * 仅保存图片文件，不存储数据库
	 */
	public static function onlySaveUploadFile($file){
	    if(empty($file['name'])){
	        throw new BusinessException('图片上传数据有误，请重试！');
	    }
	    $storagePath = self::getUploadStoragePath($file['name']);
	    self::generateImage($storagePath, file_get_contents($file['tmp_name']));
	    list($width, $height) = getimagesize($file['tmp_name']);
	    return array(
	        'width' => $width,
	        'height' => $height,
	        'url' => $storagePath
	    );
	}
	
	public static function saveUploadBakFile($fileStream, $originPath){
	    $pathInfo = pathinfo($originPath);
        $filePath = $pathInfo['dirname'].DIRECTORY_SEPARATOR.$pathInfo['filename'].'.bak.'.$pathInfo['extension'];
        self::generateImage($filePath, $fileStream);
        return self::getSaveImagePath($filePath);
	}
	
	/**
	 * 压缩打水印图片下载后删除上传图片
	 */
	public static function deleteImageByPath($filePath,$tmpFile = true){
	    $imagePath = self::getSaveImagePath($filePath);
	    if(file_exists($imagePath) && $tmpFile){
	        unlink($imagePath);
	    }
	    
	    $pathInfo = pathinfo($filePath);
	    $bakFilePath = $pathInfo['dirname'].DIRECTORY_SEPARATOR.$pathInfo['filename'].'.bak.'.$pathInfo['extension'];
	    $bakImagePath = self::getSaveImagePath($bakFilePath);
	    if(file_exists($bakImagePath)){
	        unlink($bakImagePath);
	    }
	}
	
	/**
	 * @param \haibao\cms\model\data\ImageLibrary $model
	 */
	public static function saveImageLibrary($model){
		self::checkOperation(self::FUNC_CODE_IMAGE_LIBRARY_EDIT);
		
		if(!$model->Id){
			throw new BusinessException('图片ID传递错误');
		}
		if(!$model->CategoryId){
			throw new BusinessException('图片分类不能为空');
		}
		if(!$model->Source){
			throw new BusinessException('图片来源不能为空');
		}
		if(!$model->Level){
		    throw new BusinessException('图片等级不能为空');
		}
		$model->Id = explode(',', $model->Id);
		if($model->Description){
			$model->Editor = self::getCurrentUser()->Id;
			$model->Status = \haibao\cms\model\data\ImageLibrary::STATUS_PUBLISH;
		}else{

			$model->Editor = 0;
		}
		$model->UserName = self::getCurrentUser()->UserName;
		$data = new \haibao\cms\data\ImageLibrary();
		$data->updateImageLibrary($model);
		if($model->Status == \haibao\cms\model\data\ImageLibrary::STATUS_PUBLISH){
		    $data = new \haibao\cms\data\ImageProperty();
		    $data->insertImageHeat($model->Id,$model->Level);
		}
		self::saveOperateHistory(self::FUNC_CODE_IMAGE_LIBRARY_UPLOAD, $model->toArray(true,true));
	}
	
	/**
	 * @param \haibao\cms\model\data\ImageLibrary $model
	 */
	public static function updateImageStatus($id, $status = \haibao\cms\model\data\ImageLibrary::STATUS_ENABLE){
		if(!isset($id)){
			throw new BusinessException('图片ID不能为空');
		}
		
		$data = new \haibao\cms\data\ImageLibrary();
		$data->updateImageStatus($id, $status, self::getCurrentUser()->UserName);
		
		self::saveOperateHistory(self::FUNC_CODE_IMAGE_LIBRARY_DELETE, array($id, $status));
	}
	
	public static function deleteImage($id){
		self::updateImageStatus($id, \haibao\cms\model\data\ImageLibrary::STATUS_DELETE, self::getCurrentUser()->UserName);
		self::saveOperateHistory(self::FUNC_CODE_IMAGE_LIBRARY_DELETE, array($id, \haibao\cms\model\data\ImageLibrary::STATUS_DELETE));
	}
	
	/**
	 * 彻底删除图片，包括数据库、文件存储、MongoDB中的全部内容
	 */
	public static function realDeleteImage(){
// 		$data = new \haibao\cms\data\ImageLibrary();
	}
	
	/**
	 * 得图片内容
	 * @param unknown_type $picId
	 * @return Ambigous <NULL, multitype:>
	 */
	public function getImgageById($picId){
		$data = new \haibao\cms\data\ImageLibrary();
		return $data->getImgageByMongo($picId);
	}
	
	/**
	 * 获取要上传文件的存储路径
	 * @return string /upload/2014/1230/1419842617.9557.jpg
	 */
	public static function getUploadStoragePath($orgionName){
		$basePath = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::IMAGE_UPLOAD_BASE_PATH).date('Y').DIRECTORY_SEPARATOR.date('md').DIRECTORY_SEPARATOR.microtime(true);
		$extension = pathinfo($orgionName);
		$basePath .= '.' . strtolower(isset($extension['extension']) ? $extension['extension'] : 'jpg');
		return str_replace(array('/', '\\'), '/', $basePath);
	}
	
	/**
	 * 裁剪图片
	 * @param : $saveImagePath 图片保存地址
	 * @param : $isOriginal 是否保留原图
	 */
	public static function cutImageLibrary($saveImagePath,$isOriginal,$width,$height,$imageInfo = null){
		self::createDir(\haibao\frame\Environment::path().'/'.$saveImagePath);
		
		if($isOriginal){
			
			$userId = self::getCurrentUser()->Id;
			$userName = self::getCurrentUser()->UserName;
			$imageLibraryData = new \haibao\cms\data\ImageLibrary();
			$imageId = $imageLibraryData->saveImage('..'.$saveImagePath, $saveImagePath, $userId, \haibao\cms\model\data\ImageLibrary::UPLOAD_SOURCE_CUT,$userName);	
			$model = new \haibao\cms\model\data\ImageLibrary();
			$model->Id = $imageId;
			$model->Width = $width;
			$model->Height = $height;
			$model->Description = $imageInfo->Description;
			$model->EventId = $imageInfo->EventId;
			$model->Source = $imageInfo->Source;
			$model->CategoryId = $imageInfo->CategoryId->CategoryId;
			if($imageInfo->Tags){
				$temp = array();
				foreach($imageInfo->Tags as $tag){
					array_push($temp,$tag->TagId);
				}
				if($temp){
					$model->Tags = implode(',',array_flip(array_flip($temp)));
				}
			}
			if($imageInfo->Brands){
				$temp = array();
				foreach($imageInfo->Brands as $brand){
					if($brand->BrandId){
						array_push($temp,$brand->BrandId);
					}
				}
				if($temp){
					$model->Brands = implode(',',array_flip(array_flip($temp)));
				}
			}
			if($imageInfo->Persons){
				$temp = array();
				foreach($imageInfo->Persons as $person){
					if($person->Name){
						array_push($temp,$person->Name);
					}
				}
				if($temp){
					$model->Persons = implode(',',array_flip(array_flip($temp)));
				}
			}
			self::saveImageLibrary($model);
		}else{
			
			$imageLibrary = new \haibao\cms\data\ImageLibrary();
			$imageInfo->Width = $width;
			$imageInfo->Height = $height;
			$imageInfo->Filename = $saveImagePath;
			$imageInfo->PicId = 0;
			$imageLibrary->updateImageLibrary($imageInfo);
		}
		return $saveImagePath;
	}
	
	/**
	 * 保存图片到存储
	 * @param int $id
	 * @param string $imageUrl
	 */
	private static function saveImage2Storage($id, $imageUrl){
		$data = new \haibao\cms\data\ImageLibraryMongo();
		$collection = $data->getFileById($id, $imageUrl);
		$imageContent = $collection->getBytes();
		$fileArr = (array)$collection;
		self::generateImage($fileArr['file']['Filename'], $imageContent);
	}
	
	/**
	 * 保存生图片
	 */
	public static function generateImage($path, $stream){
		$savePath = self::getSaveImagePath($path);
		self::createDir($savePath);
		file_put_contents($savePath, $stream, LOCK_EX);
	}
	
	/**
	 * 获取文件保存路径
	 */
	public static function getSaveImagePath($path){
		$path = '/'. ltrim($path,'/');
		return \haibao\frame\Environment::path() . $path;
	}
	
	/**
	 * 获取文件保存路径
	 */
	public static function getSaveUploadPath($path){
		$path = '/'. ltrim($path,'/');
		return \haibao\frame\Environment::path().'/tmppic' . $path;
	}
	
	/**
	 * 创建文件保存路径
	 */
	public static function createDir($path){
		$path = pathinfo( $path );
		return is_writable($path['dirname']) ?: mkdir($path['dirname'], 750, true);
	}
	
	private static function dataConvert(&$imageList){
		foreach ($imageList as $key=>$list){
			$fileNameKey = \haibao\cms\model\filter\ImageLibrary::CONDITION_FIELD_NAME_FILENAME;
			$picIdKey = \haibao\cms\model\filter\ImageLibrary::CONDITION_FIELD_NAME_PICID;
			$list->$fileNameKey = ltrim($list->$fileNameKey,'/');
			$list->$fileNameKey = \haibao\frame\Environment::getConfig('APP_DOMAIN').$list->$fileNameKey;
		}
	}
	
	/**
	 * 检查图片是否存在，没有从MongoDB中取出后生成图片
	 */
	private static function checkImage($MogoId, $imageUrl){
		if(!$MogoId){
			return false;
		}
		$path = self::getSaveImagePath($imageUrl);
		
		$fullUrl = \haibao\frame\http\HttpEnvironment::domain().$imageUrl;
		if(!file_exists($path)){
			$data = new \haibao\cms\data\ImageLibraryMongo();
			$collection = $data->getFileById($MogoId, $imageUrl);
			if($collection){
				$stream = $collection->getBytes();
				self::generateImage($imageUrl, $stream);
			}
		}
	}
	/**
	 * 图片统计
	 */
	public static function getImageStatisticList($queryArr){
		$data = new \haibao\cms\data\ImageLibrary();
		return $data->getImageStatisticList($queryArr);
	}
	/**
	 * 修改聚焦显示
	 */
	public static function setFocusShow($infoId,$imageIds,$infoType){
		$data = new \haibao\cms\data\ImageLibrary();
		return $data->setFocusShow($infoId,$imageIds,$infoType);
	}
	
	/**
	 * 获取app图片信息 到CMS库
	 */
	public static function addImageToCms($images){
		$imageIds = array();
		foreach($images as $key=>$val){
			$imageIds[] = $val->Id;
		}
		$appImageLibraryBusiness = new \haibao\app\business\ImageLibrary();
		$images = $appImageLibraryBusiness::getImagesById($imageIds);

		foreach($images as $key=>$val){
			$val->UpSource = \haibao\cms\model\data\ImageLibrary::UPLOAD_SOURCE_IMAGE_LIBRARY;
			$val->CreateBy = self::getCurrentUser()->Id;
			$val->CreateTime = new \DateTime();
			$val->Status = \haibao\cms\model\data\ImageLibrary::STATUS_DISABLE;
		}
		$data = new \haibao\cms\data\ImageLibrary();
		$data->addImageToCms($images);
	}
	
	/**
	 * 保存水印地址
	 */
	public static function saveWatermarkUrl($url){
	    $data = new \haibao\cms\data\ImageLibrary();
	    $data->saveWatermarkUrl($url);
	}
	
	public static function getWatermarkUrl(){
	    $data = new \haibao\cms\data\ImageLibrary();
	    return $data->getWatermarkUrl();
	}
	
	public static function addAppImageToPc($imageArr){
	    if(!$imageArr){
	        return false;
	    }
	    $imageInfo = \haibao\app\business\ImageLibrary::getImagesById($imageArr);
	    foreach($imageInfo as $key=>$val){
	        $imageInfo[$key]->CreateTime = new \DateTime();
	        $imageInfo[$key]->Source = \haibao\cms\model\data\ImageLibrary::UPLOAD_SOURCE_IMAGE_LIBRARY;
	        $imageInfo[$key]->UpSource = \haibao\cms\model\data\ImageLibrary::UPLOAD_SOURCE_IMAGE_LIBRARY;
	        $imageInfo[$key]->Status = \haibao\app\model\data\ImageLibrary::STATUS_ENABLE;
	        $imageInfo[$key]->CreateBy = self::getCurrentUser()->Id;
	    }
	    $data = new \haibao\cms\data\ImageLibrary();
	    return array($data->addImageToCms($imageInfo),$imageInfo);
	}

	public static function updateImageIndexTime($imageIds){
		$data = new \haibao\cms\data\ImageLibrary();
		$data->updateImageIndexTime($imageIds);
	}

	public static function setWapImageIdsStorage($name,$userId,$ids){
	    $data = new \haibao\cms\data\ImageLibrary();
	    $data->setWapImageIdsStorage($name,$userId,$ids);
	}
	
	public static function getWapImageIdsStorage($name,$userId){
	    $data = new \haibao\cms\data\ImageLibrary();
	    return $data->getWapImageIdsStorage($name,$userId);
	}

	public static function deleteWapImageIdsStorage($name,$userId){
		$cache = new \haibao\cms\cache\ImageLibrary();
		return $cache->delWapMarkImageList($name,$userId);
	}

	public static function getImageSourceNames(){
		$data = new \haibao\cms\data\ImageLibrary();
		return $data->getImageSourceNames();
	}
	
	public static function getImageByFilename($filename){
        $data = new \haibao\cms\data\ImageLibrary();
        return $data->getImageByFilename($filename);
    }

    public static function addImageByModel($model){

        $data = new \haibao\cms\data\ImageLibrary();
        return $data->addImageByModel($model);
    }
	
}

